package opl.services;

import java.util.Timer;

import opl.common.ConfigurationSettings;

import android.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class NotificationService extends Service {

	private static final String ACTION_NOTIFICATION_SERVICE = "opl.services.NotificationService";
	private Timer tmrNotifications;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		int interval = intent.getIntExtra(ConfigurationSettings.SYNC_INTERVAL,
				2);
		interval = interval * 1000 * 60;

		tmrNotifications = new Timer();

		tmrNotifications.scheduleAtFixedRate(new NotificationTimerTask(this,
				interval), 10000, interval);
	}

	@Override
	public void onDestroy() {
		tmrNotifications.cancel();
		super.onDestroy();
	}

	public static boolean isServiceRunning(Context c) {
		ActivityManager actMgr = (ActivityManager) c
				.getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo info : actMgr
				.getRunningServices(Integer.MAX_VALUE)) {
			if (ACTION_NOTIFICATION_SERVICE.equalsIgnoreCase(info.service
					.getClassName())) {
				return true;
			}
		}
		return false;
	}
}
